package com.sywb.chuangyebao;

import android.app.Activity;
import android.content.Intent;

import com.sywb.chuangyebao.bean.VersionInfo;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.dialog.AppUpgradeDialog;

import org.bining.footstone.BaseUpdateService;
import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.ApkUtils;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.ToastUtils;

/**
 * 检查更新后台下载服务
 */
public class MyUpdateService extends BaseUpdateService {

    private MyApplication application;
    // 是否强制更新
    private boolean isForceUpdate;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        application = (MyApplication) getApplication();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void checkUpdate() {
        String appmark = ApkUtils.getMetaData(getBaseContext(), "UMENG_CHANNEL");
        HttpUtils.getVersion(appmark, new HttpCallback<VersionInfo>() {
            @Override
            public void onSuccess(VersionInfo versionInfo) {
                SharedUtils.put(Constants.ISHAVEVERSION, false);
                if (versionInfo != null) {
                    if (versionInfo.appnum > ApkUtils.getVersionCode()) {
                        SharedUtils.put(Constants.ISHAVEVERSION, true);
                        isForceUpdate = versionInfo.isupdate == 1;// 是否强制更新
                        Activity activity = application.getAppComponent().appManager().getCurrentActivity();
                        if (activity != null && !activity.isFinishing()) {
                            AppUpgradeDialog hintDialog = AppUpgradeDialog.newInstance("软件更新", versionInfo.remark, isForceUpdate, versionInfo.appurl);
                            hintDialog.setOnItemListener(new AppUpgradeDialog.OnItemListener() {
                                @Override
                                public void onResult(int postion, boolean isForceUpdate, String apkUrl) {
                                    switch (postion) {
                                        case 1:
                                            if (isForceUpdate) {
                                                //版本为强制更新，点击取消则直接退出App
                                                application.getAppComponent().appManager().killAll();
                                                // exit("请更新后再使用APP");
                                            } else {
                                                exit("暂不更新");
                                            }
                                            break;
                                        case 2:
                                            download(apkUrl);
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            });
                            hintDialog.show(activity.getFragmentManager(), "VersionUpdate");
                            return;
                        }
                    }
                }
                exit("当前已是最新版本");
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                SharedUtils.put(Constants.ISHAVEVERSION, false);
                exit(strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                initFileConfig();
            }
        });
    }

    @Override
    public void postProgress(String s) {
    }

    /**
     * 初始化文件配置
     */
    public void initFileConfig() {
        HttpUtils.getFileConfig(new HttpCallback<String>() {
            @Override
            public void onSuccess(String s) {
                SharedUtils.put(Constants.USERFILECONFIG, s);
            }
        });
        HttpUtils.getAppConfig(new HttpCallback<String>() {
            @Override
            public void onSuccess(String s) {
                SharedUtils.put(Constants.APPCONFIG, s);
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                Logger.e(strMsg);
            }
        });
    }

    /**
     * 停止更新服务
     */
    public void exit(String message) {
        MyUpdateService.this.stopSelf();
        if (isForceUpdate) {
            ToastUtils.show(getBaseContext(), "此次更新了较多内容，请完成本次更新后再使用App");
            // application.exit();
        }
        RxBus.get().post(Constants.POST_ABOUT_VERSION, message);
    }

}
